#ifndef _GOVT_DB_HPP_
#define _GOVT_DB_HPP_
/*******************************************************************************
 ** Government Sanctioned Espionage RPG                                       **
 ** http://www.government-sanctioned.us/                                      **
 **===========================================================================**
 ** Name: db.hpp                                                              **
 ** Description: ODB ORM DB Services                                          **
 **                                                                           **
 ** Open Source Initiative (OSI) Approved License                             **
 **                                                                           **
 ** The contents of this file are subject to the terms of the                 **
 ** Common Development and Distribution License, Version 1.0 only             **
 ** (the "License").  You may not use this file except in compliance          **
 ** with the License.                                                         **
 **                                                                           **
 ** You can find a copy of the license in the LICENSE file within             **
 ** this distribution or at $WIKI/display/GOVT/License-software.              **
 ** Software distributed under the License is distributed on an "AS IS"       **
 ** basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.           **
 ** See the License for the specific language governing permissions           **
 ** and limitations under the License.                                        **
 **                                                                           **
 ** When distributing Covered Code, include this CDDL header in each          **
 ** file and include the License file at $GAME_DIR/LICENSE.                   **
 ** If applicable, add the following below this header, with the indicated    **
 ** fields enclosed by brackets "[]" replaced with your own identifying       **
 ** information: Portions Copyright [yyyy] [name of copyright owner]          **
 **                                                                           **
 ** Copyright (c) 2009-2012  Barry Gian James <gian@gamingods.net>            **
 ** All rights reserved.
 **                                                                           **
 ** Ref: $HeadURL$
 ******************************************************************************/

// $Id$
// Last Modified by $Author$

#ifdef USE_MONGODB
#include <mongo/client/dbclient.h>
#endif

#include <odb/database.hxx>
#include <odb/mysql/database.hxx>
#include <boost/shared_ptr.hpp>

#include <iostream>
#include <fstream>
#include <map>
#include <string>

#include "common.hpp"

#define DEFAULT_SERVER_CONF "sanctiond.conf"

//! @class DataBase
//! @brief The high-level db management class
template<class T>
class DataBase
{
public:
//	explicit DataBase(const std::string &);
	DataBase();

	boost::shared_ptr<odb::database> & DB() { return db; }
	boost::shared_ptr<T> Load(entity_t id) { return db->load<T>(id); }
	void Update(const T & o) { db->update(o); }
	void Persist(const T & o) { db->persist(o); }

private:
	boost::shared_ptr<odb::database> db;
};

#ifdef USE_MONGODB
// http://www.mongodb.org/pages/viewpage.action?pageId=133415
class MongoDB
{
public:
	MongoDB(const std::string & d) : db(d) { }
	void Insert(const std::string & collection, const std::string & json);

private:
	std::string db;
};
#endif

#define BEGIN_TRANSACTION(db)	(transaction __t(db->DB()->begin()))
#define END_TRANSACTION	(__t.commit())

//! @class ServerCfg
//! @brief Reads short server cfg file for sensitive data
//! This is a very small configuration database that is used
//! to keep from passing passwords to the server on the command line
class ServerCfg
{
public:
    ServerCfg(const std::string & f = DEFAULT_SERVER_CONF);

    bool        Init();
    int         Parse();

    std::string & GetValue(const std::string & a) { return avpairs[a]; }
    void        AddAVPair(const std::string &a, const std::string & v) { avpairs[a] = v; }

private:
    std::map<std::string,std::string>    avpairs;
    std::ifstream    conf;
    std::string		fl;
    bool			bInitialized;

};





#endif /* _GOVT_DB_HPP_ */
